package com.service.impl;

import com.dao.RbacRoleDao;
import com.entity.RbacRole;
import com.entity.RbacRoleMenuDTO;
import com.entity.ResponseEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RbacRoleServiceImpl implements RbacRoleService {
    private RbacRoleDao rbacRoleDao;
    @Autowired
    public void setRbacRoleDao(RbacRoleDao rbacRoleDao) {
        this.rbacRoleDao = rbacRoleDao;
    }

    @Override
    public ResponseEntity<RbacRole> findAll() {
        List<RbacRole> list = rbacRoleDao.findAll();
        return new ResponseEntity(200,"查询成功",list);
    }

    @Override
    public String findByRbacManager(String username) {
        return rbacRoleDao.findByRbacManger(username);
    }



    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        //分页
        PageHelper.startPage(currentPage,pageSize);
        List<RbacRole> rbacRoleList = rbacRoleDao.findAll();
        PageInfo pageInfo = new PageInfo<>(rbacRoleList);
        return new ResponseEntity<>(200,"执行成功",pageInfo);
    }

    @Override
    public ResponseEntity checkedRole(int roleId) {
        List<Integer> checked = rbacRoleDao.checkedRole(roleId);
        return new ResponseEntity<>(200,"执行成功",checked);
    }

    @Override
    @Transactional//需要事件
    public ResponseEntity changeMenus(RbacRoleMenuDTO rbacRoleMenuDTO) {
        //考虑下如何保存数据？？？先删除初始数据再全部新增进去
        int i = rbacRoleDao.delete(rbacRoleMenuDTO.getRoleId());
        int j = rbacRoleDao.saveChangeMenus(rbacRoleMenuDTO.getRoleId(),rbacRoleMenuDTO.getModuleIds());
        if (j>0){
            return new ResponseEntity<>(200,"成功更新权限菜单数据",null);
        }
        return ResponseEntity.FAIL;
    }
}
